package com.qersoft.controller;

import com.qersoft.entity.ClassMemberEntity;
import com.qersoft.entity.ResponseMessage;
import com.qersoft.service.ClassMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/class-member")
public class ClassMemberController {

    @Autowired
    private ClassMemberService classMemberService;

    /**
     * 添加班级成员
     * @param classMemberEntity 班级成员实体
     * @return 添加后的班级成员
     */
    @PostMapping("/add")
    public ResponseMessage<ClassMemberEntity> addClassMember(@RequestBody ClassMemberEntity classMemberEntity) {
        ClassMemberEntity createdClassMember = classMemberService.addClassMember(classMemberEntity);
        return ResponseMessage.message(createdClassMember);
    }

    /**
     * 获取班级成员列表
     * @param classId 班级ID
     * @return 班级成员列表
     */
    @GetMapping("/class/{classId}")
    public ResponseMessage<List<ClassMemberEntity>> getClassMembers(@PathVariable Long classId) {
        List<ClassMemberEntity> classMembers = classMemberService.getClassMembers(classId);
        return ResponseMessage.message(classMembers);
    }

    /**
     * 更新班级成员信息
     * @param classMemberEntity 班级成员实体
     * @return 更新结果
     */
    @PutMapping("/update")
    public ResponseMessage<Boolean> updateClassMember(@RequestBody ClassMemberEntity classMemberEntity) {
        boolean result = classMemberService.updateClassMember(classMemberEntity);
        return ResponseMessage.message(result);
    }

    /**
     * 删除班级成员
     * @param classMemberId 班级成员ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{classMemberId}")
    public ResponseMessage<Boolean> deleteClassMember(@PathVariable Long classMemberId) {
        boolean result = classMemberService.deleteClassMember(classMemberId);
        return ResponseMessage.message(result);
    }

    /**
     * 获取班级成员详细信息
     * @param memberId 班级成员ID
     * @return 班级成员信息
     */
    @GetMapping("/{memberId}")
    public ResponseMessage<ClassMemberEntity> getClassMemberById(@PathVariable Long memberId) {
        ClassMemberEntity classMemberEntity = classMemberService.getClassMemberById(memberId);
        if (classMemberEntity != null) {
            return ResponseMessage.message(classMemberEntity);
        } else {
            return ResponseMessage.message(null);  // 如果没有找到成员，返回null
        }
    }
}
